package com.videolist.lingling.ui;

import android.app.Fragment;
import android.app.FragmentManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.videolist.lingling.ContentFragment;
import com.videolist.lingling.R;
import com.videolist.lingling.adapter.ItemClickListener;
import com.videolist.lingling.adapter.Section;
import com.videolist.lingling.adapter.SectionedExpandableLayoutHelper;
import com.videolist.lingling.models.Item;

import java.util.ArrayList;
import java.util.List;

public class DrawerActivityActivity extends AppCompatActivity implements ItemClickListener{

//    private ListView listView;

    private DrawerLayout drawerLayout;

    private TextView textView;
    /*创建一个Drawerlayout和Toolbar联动的开关*/
    private ActionBarDrawerToggle toggle;
    private android.support.v7.widget.Toolbar toolbar;
    final List<String> list = new ArrayList<String>();
    RecyclerView mRecyclerView;
    ArrayList<Item> arrayList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rtsp2);
        initView();
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setActionBar();
            /*设置Drawerlayout开关*/
        setDrawerToggle();
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
SectionedExpandableLayoutHelper sectionedExpandableLayoutHelper = new SectionedExpandableLayoutHelper(DrawerActivityActivity.this, mRecyclerView, DrawerActivityActivity.this, 1);

        //random data

        arrayList.add(new Item("iPhone", 0));
        arrayList.add(new Item("iPad", 1));
        arrayList.add(new Item("iPod", 2));
        arrayList.add(new Item("iMac", 3));
        sectionedExpandableLayoutHelper.addSection("Apple Products", arrayList);
        arrayList = new ArrayList<>();
        arrayList.add(new Item("LG", 0));
        arrayList.add(new Item("Apple", 1));
        arrayList.add(new Item("Samsung", 2));
        arrayList.add(new Item("Motorola", 3));
        arrayList.add(new Item("Sony", 4));
        arrayList.add(new Item("Nokia", 5));
        sectionedExpandableLayoutHelper.addSection("Companies", arrayList);
        arrayList = new ArrayList<>();
        arrayList.add(new Item("Chocolate", 0));
        arrayList.add(new Item("Strawberry", 1));
        arrayList.add(new Item("Vanilla", 2));
        arrayList.add(new Item("Butterscotch", 3));
        arrayList.add(new Item("Grape", 4));
        sectionedExpandableLayoutHelper.addSection("Ice cream", arrayList);

        sectionedExpandableLayoutHelper.notifyDataSetChanged();

        //checking if adding single item works
        sectionedExpandableLayoutHelper.addItem("Ice cream", new Item("Tutti frutti",5));
        sectionedExpandableLayoutHelper.notifyDataSetChanged();
//        drawerLayout.openDrawer(Gravity.LEFT);//侧滑打开  不设置则不会默认打开*/
    }

    /*设置ActionBar*/
    private void setActionBar() {
        setSupportActionBar(toolbar);
        /*显示Home图标*/
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    /*设置Drawerlayout的开关,并且和Home图标联动*/
    private void setDrawerToggle() {
        toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, 0, 0);
        drawerLayout.addDrawerListener(toggle);
        /*同步drawerlayout的状态*/
        toggle.syncState();
    }

    private void initView() {
//        listView = (ListView) findViewById(R.id.v4_listview);
        drawerLayout = (DrawerLayout) findViewById(R.id.v4_drawerlayout);
        drawerLayout.setScrimColor(Color.TRANSPARENT);
        textView = (TextView) findViewById(R.id.v4_text);
//        initDate();
    }

    private void initDate() {

        list.add("网易");
        list.add("腾讯");
        list.add("新浪");
        list.add("搜狐");
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
   /*     listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                textView.setText(list.get(position));
//                showDrawerLayout();

            }
        });*/

    }

    private void showDrawerLayout() {
        if (!drawerLayout.isDrawerOpen(Gravity.LEFT)) {
            drawerLayout.openDrawer(Gravity.LEFT);
        } else {
            drawerLayout.closeDrawer(Gravity.LEFT);
        }
    }


    @Override
    public void itemClicked(Item item) {
        showDrawerLayout();
        Fragment contentFragment = new ContentFragment();
        Bundle args = new Bundle();
        args.putString("text", "");
        contentFragment.setArguments(args);

        FragmentManager fm = getFragmentManager();
        fm.beginTransaction().replace(R.id.v4_drawerlayout_frame, contentFragment)
                .commit();

        drawerLayout.closeDrawer(Gravity.LEFT);
    }

    @Override
    public void itemClicked(Section section) {

    }
}
